﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;


namespace TableModule
{
    [XmlInclude(typeof(TableSystemSetting))]
    public class TableSystemSetting
    {
        public TableItemSetting tableSetting;
        public TableSystemSetting()
        {
            tableSetting = new TableItemSetting();
            
        }
        public static TableSystemSetting Load()
        {
            TableSystemSetting pDoc=null;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableSystemSetting));
            FileStream fsReader = null;
            try
            {
                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml");
                pDoc = (TableSystemSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
            }
            catch(IOException)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                pDoc = new TableSystemSetting();
            }
            catch (Exception eMy)
            {
                if (fsReader != null)
                {
                    fsReader.Close();
                }
                MessageBox.Show(eMy.ToString() + "即将打开备份文件", Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml");

                fsReader = File.OpenRead(Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml.bak");
                pDoc = (TableSystemSetting)xmlSerializer.Deserialize(fsReader);
                fsReader.Close();
                pDoc.Save();
            }
            return pDoc;
        }
        public bool Save()
        {
            if (!Directory.Exists(Application.StartupPath + @"/MuduleParameter/"))
            {
                Directory.CreateDirectory(Application.StartupPath + @"/MuduleParameter/");
            }
            FileStream fsWriter = new FileStream(Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml", FileMode.Create, FileAccess.Write, FileShare.Read);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TableSystemSetting));
            xmlSerializer.Serialize(fsWriter, this);
            fsWriter.Flush(true);
            fsWriter.Close();

            File.Copy(Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml", Application.StartupPath + @"/MuduleParameter/TableSystemSetting" + ".xml.bak", true);
           

            return true;
        }
    }
}
